package com.work.straighttalk.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.work.straighttalk.R;
import com.work.straighttalk.app.BaseActivity;
import com.work.straighttalk.bean.Contact;
import com.work.straighttalk.presenter.ContactMesEditorPImpl;
import com.work.straighttalk.presenter.i.LoadDataPresenterI;
import com.work.straighttalk.ui.i.BindDataVI;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by dllo on 16/3/17.
 */
public class ContactsMesEditorActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener, BindDataVI<Contact> {
    @Bind(R.id.contact_avater)
    SimpleDraweeView contactAvater;
    @Bind(R.id.checkbox_num)
    CheckBox checkboxNum;
    @Bind(R.id.checkbox_click)
    RelativeLayout checkboxClick;
    @Bind(R.id.tv_ok)
    TextView tvOk;
    @Bind(R.id.tv_chancel)
    TextView tvChancel;
    @Bind(R.id.top_bar_back)
    ImageView topBarBack;
    @Bind(R.id.tv_contact_name)
    TextView tvContactName;
    @Bind(R.id.tv_contact_num)
    TextView tvContactNum;
    @Bind(R.id.bottom_bar)
    LinearLayout bottomBar;
    private boolean isNumIncluded = false;
    private LoadDataPresenterI<Uri> loadDataPresenterI;
    private Contact contact;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contacts_editor);
        ButterKnife.bind(this);
        checkboxNum.setOnCheckedChangeListener(this);
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra(getPackageName());
        Uri uri = bundle.getParcelable("ContactUri");
        loadDataPresenterI = new ContactMesEditorPImpl(this);
        loadDataPresenterI.loadData(uri);
    }

    /**
     * checkbox所在布局点击事件
     */
    @OnClick(R.id.checkbox_click)
    protected void onCheckLayoutClick() {
        if (isNumIncluded) {
            isNumIncluded = false;
            checkboxNum.setChecked(isNumIncluded);
        } else {
            isNumIncluded = true;
            checkboxNum.setChecked(isNumIncluded);
        }
    }

    /**
     * topbarback 点击事件
     */
    @OnClick(R.id.top_bar_back)
    protected void topBarBack() {
        finish();
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        isNumIncluded = isChecked;
    }

    @OnClick(R.id.tv_ok)
    protected void onOkPressed() {
        if (!isNumIncluded) {
            contact.setPhoneNumber(null);
        }
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putParcelable("ContactMes",contact);
        intent.putExtra("ContactMes",bundle);
        setResult(1,intent);
//        startActivity();
    }

    @Override
    public void bindData(Contact contact) {
        this.contact = contact;
        Uri avaterUri = Uri.parse(contact.getAvater());
        contactAvater.setImageURI(avaterUri);
        tvContactName.setText(contact.getName());
        tvContactNum.setText(contact.getPhoneNumber());
    }
}
